﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace SimpleQueryTool.Library.Data
{
	public class ExecuteSqlCommand
	{
		public DataTable Execute(string providerName, string connectionstring, string sql)
		{
			var dataTable = new DataTable();


			//var classes = DbProviderFactories.GetFactoryClasses();
			

			var dbFactory = DbProviderFactories.GetFactory(providerName);
			
			using (var dbCommand = dbFactory.CreateCommand())
			{
				dbCommand.CommandText = sql;
				dbCommand.CommandType = CommandType.Text;
				dbCommand.Connection = dbFactory.CreateConnection();
				dbCommand.Connection.ConnectionString = connectionstring;
				dbCommand.Connection.Open();

				using (var reader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection))
				{
					dataTable.Load(reader);
					reader.Close();
				}
			}

			return dataTable;
		}
	}
}
